package org.xu.controller;

import com.alibaba.fastjson.JSONObject;
import com.core.base.PagedReply;
import com.core.base.Reply;
import com.core.utils.UUIDGenerator;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xu.bo.DeepCopyBO;
import org.xu.dto.UserInfoDTO;
import org.xu.dto.WorkFlowLogDTO;
import org.xu.entity.WorkFlowLogEntity;
import org.xu.request.WorkLogRequest;
import org.xu.request.WorkPageRequest;
import org.xu.service.WorkFlowLogService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executor;

@Slf4j
@RequestMapping("/")
@Controller
public class TestController {

    @Autowired
    private WorkFlowLogService workFlowLogService;
    @Autowired
    @Qualifier("IOTaskExecutorPool")
    private Executor executor;

    @ResponseBody
    @RequestMapping("/testBigData")
    public Reply<String> testBigData(){
        executor.execute(() ->{
            log.info("{}",1111);
        });
        return Reply.success("finish");
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<WorkFlowLogEntity> list(){
        List<WorkFlowLogEntity> list = workFlowLogService.list();
        log.info("list:{}",list);
        log.info("~==== ----------");
        log.info("list:{}",JSONObject.toJSONString(list));
        return list;
    }

    @ResponseBody
    @RequestMapping("/lockGet")
    public Reply<List<WorkFlowLogEntity>> lockGet(){
        return workFlowLogService.lockGet();
    }

    /**
     * 测试序列化，反序列化
     */
    @RequestMapping("/testJsonSerialize")
    @ResponseBody
    public Reply<List<WorkFlowLogDTO>> testJsonSerialize(@RequestBody WorkLogRequest request){
        List<WorkFlowLogDTO> list = workFlowLogService.query(request);
        return Reply.success(list);
    }

    /**
     * 测试分页查询
     */
    @RequestMapping("/pageQuery")
    @ResponseBody
    public PagedReply<WorkFlowLogDTO> pageQuery(){
        WorkPageRequest request = new WorkPageRequest();
        request.setIndex(1);
        request.setPageSize(200);
        request.setApplyNo("test_");
        PageInfo<WorkFlowLogDTO> pageInfoList = workFlowLogService.pageQuery(request);
        return new PagedReply<>(request.getIndex(), request.getPageSize(), pageInfoList.getTotal(), pageInfoList.getList());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Reply<String> delete(){
       workFlowLogService.deleteHistoryData(LocalDate.now());
       return Reply.success("6");
    }

    /**
     * 测试深拷贝
     */
    @RequestMapping("/testDeepClone")
    @ResponseBody
    public Reply<String> testDeepClone(){
        DeepCopyBO bo = new DeepCopyBO();
        bo.setAge((short) 125);
        bo.setId(100);
        bo.setCreateDate(new Date());
        bo.setLocalDateTime(LocalDateTime.now().plusHours(1));
        bo.setName("张三");
        DeepCopyBO cloneBO = SerializationUtils.clone(bo);
        log.info("深拷贝测试: cloneBO == bo {},Id :{},name:{},age:{}", cloneBO == bo, cloneBO.getId().equals(bo.getId()),cloneBO.getName() == bo.getName(),cloneBO.getAge() == bo.getAge());
        log.info("拷贝完成:{}", JSONObject.toJSONString(cloneBO));
        return Reply.success("测试深拷贝");
    }

    @ResponseBody
    @RequestMapping("/user")
    public Reply<UserInfoDTO> userInfo(){
        String [] array = new String[] {"张三","法外狂徒","李四","张飞","飞飞","王老七"};
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        int randUserIndex = new Random().nextInt(100) % 6;
        int randLogIndex = new Random().nextInt(100);
        String fileName = randLogIndex % 12 + ".jpg";
        userInfoDTO.setUserName(array[randUserIndex]);
        userInfoDTO.setLogoPicUrl("http://localhost:9090/"+ fileName);
        userInfoDTO.setFansCount(randLogIndex);
        userInfoDTO.setFollowCount(randUserIndex);
        userInfoDTO.setNewsCount(randUserIndex + 2);
        return Reply.success(userInfoDTO);
    }

    @ResponseBody
    @RequestMapping("/logInsert")
    public Reply<String> logInsert(){
        WorkFlowLogEntity record = new WorkFlowLogEntity();
        record.setApplyNo(UUIDGenerator.generate());
        record.setRemark("ceshi");
        workFlowLogService.insert(record);
        return Reply.success("1");
    }

    public static void main(String[] args) {
        String a = "ab";
        String b = "a" + "b";
        System.out.println(a == b);

        LocalDate date1 = LocalDate.of(2022, 9, 25);
        LocalDate date2 = LocalDate.now();
        //年差
        int years = date1.getYear() - date2.getYear();
        //月差
        int months = years * 12 + (date1.getMonthValue() - date2.getMonthValue());
        //天差
        long days = date1.toEpochDay() - date2.toEpochDay();
        long days2 = date1.getDayOfMonth() - date2.getDayOfMonth();

        System.out.println(years);
        System.out.println(months);
        System.out.println(days);
        System.out.println(days2);
        String aa = "hello";
        String aa1 = aa("aa");
        System.out.println(aa1);
        System.out.println(aa);
    }


    private static String aa (String abc){
        abc = "123";
        return  abc;
    }

}
